﻿#region Using

using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Serialization;

using Common.Base;
using Common.Enums;

using GameCommon.Converters;

using Menu.Components;

using Microsoft.Xna.Framework;

#endregion

namespace Menu.XmlEntities
{
    [Serializable]
    public class XmlContainer
    {
        private Color backgroundColor;

        public XmlContainer()
        {
            this.Components = new List<XmlBaseEntity>();
        }

        [XmlAttribute("Id")]
        public string Id { get; set; }

        [XmlAttribute("Opacity")]
        public int Opacity { get; set; }

        [XmlAttribute("BackgroundColor")]
        public string BackgroundColor
        {
            get
            {
                return this.backgroundColor.ToString();
            }

            set
            {
                this.backgroundColor = Converter.Color.Convert(value);
            }
        }

        [XmlAttribute("X")]
        public int X { get; set; }

        [XmlAttribute("Y")]
        public int Y { get; set; }

        [XmlAttribute("Width")]
        public int Width { get; set; }

        [XmlAttribute("Height")]
        public int Height { get; set; }

        [XmlAttribute("SizeModifier")]
        public SizeModifier SizeModifier { get; set; }

        [XmlElement("TextInput", typeof(XmlTextInput), IsNullable = false)]
        [XmlElement("Texture", typeof(XmlTexture), IsNullable = false)]
        [XmlElement("Text", typeof(XmlText), IsNullable = false)]
        [XmlElement("SpriteTexture", typeof(XmlSpriteTexture), IsNullable = false)]
        public List<XmlBaseEntity> Components { get; set; }

        internal IEnumerable<MenuComponentBase> GetComponent()
        {
            var list = new List<MenuComponentBase>();
            var comp = new Container(this.Id, this.SizeModifier);

            comp.Opacity = (float)(this.Opacity / 100.0);
            comp.Dimension = new Dimension(this.X, this.Y, this.Width, this.Height);
            comp.BackgroundColor = this.backgroundColor;

            comp.Settle();

            list.Add(comp);
            list.AddRange(this.Components.Select(component => component.GetComponent(comp)));

            return list;
        }
    }
}